﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using SubSonic;

namespace Business
{
     public class DelayCalculations
    {
         public  static int Delay(Guid TaskId , DateTime date)
         {
             TblTask task = new TblTask();
             task = TblTask.FetchByID(TaskId);
             TblTaskCollection taskCollection = new TblTaskCollection();
             Query query = new Query(TblTask.Schema);
             int delay;
             if (task.SurveyorStepId.HasValue)
             {
                 //Temporarily commented to make table ok for Surveyors. TblSurveyors must be ok first.

                 //TblStepsSurveyor stepsSurveyor = task.TblStepsSurveyor;
                 //TblStepsSurveyorCollection tblStepsSurveyorCollection = new TblStepsSurveyorCollection();
                 //if (stepsSurveyor.Priority == 1)
                 //    return 0;
                 //tblStepsSurveyorCollection.LoadAndCloseReader(TblStepsSurveyor.FetchByParameter(TblStepsSurveyor.Columns.Priority, stepsSurveyor.PreviousStep));
                 //TblStepsSurveyor prevStepSurveyor = tblStepsSurveyorCollection.First();
                 //TblTask prevTask = new TblTask();
                 //query.AddWhere(TblTask.Columns.ContractId, task.ContractId);
                 //query.AddWhere(TblTask.Columns.SurveyorStepId, prevStepSurveyor.Id);
                 //query.QueryType = QueryType.Select;
                 //query.OrderBy = SubSonic.OrderBy.Desc(TblTask.Columns.DateX);
                 //query.BuildSelectCommand();
                 //prevTask.LoadAndCloseReader(query.ExecuteReader());
                 //TimeSpan timeSpan = new TimeSpan();
                 //timeSpan = Convert.ToDateTime(task.DateX) - Convert.ToDateTime(prevTask.DateX);
                 //delay = timeSpan.Days - Convert.ToInt32(stepsSurveyor.DeadLineDays);
                 //return delay > 0 ? delay : 0;
             }
             if (task.AdvisorStepId.HasValue)
             {
                 TblStepsAdvisor stepsAdvisor = task.TblStepsAdvisor;
                 if (stepsAdvisor.Priority == 1)
                     return 0;
                 if (!stepsAdvisor.PreviousStep.HasValue || (stepsAdvisor.PreviousStep == 0))
                 {
                     return 0;
                 }
                 TblStepsAdvisor prevStepAdvisor = new TblStepsAdvisor();
                 TblStepsAdvisorCollection tblStepsAdvisorCollection = new TblStepsAdvisorCollection();

                 tblStepsAdvisorCollection.LoadAndCloseReader(TblStepsAdvisor.FetchByParameter(TblStepsAdvisor.Columns.Priority, stepsAdvisor.PreviousStep));
                 prevStepAdvisor = tblStepsAdvisorCollection.First();
                 TblTask prevTask = new TblTask();
                 Query query2 = new Query(TblTask.Schema);
                 query2.AddWhere(TblTask.Columns.AdvisorStepId, prevStepAdvisor.Id);
                 query2.AddWhere(TblTask.Columns.ContractId, task.ContractId);
                 query2.QueryType = QueryType.Select;
                 query2.OrderBy = SubSonic.OrderBy.Desc(TblTask.Columns.DateX);
                 query2.BuildSelectCommand();
                 taskCollection.LoadAndCloseReader(query2.ExecuteReader());
                 prevTask = taskCollection.First();
                 TimeSpan timeSpan = new TimeSpan();
                 if (prevTask.DateX.HasValue)
                 {
                     timeSpan = date - Convert.ToDateTime(prevTask.DateX);
                     delay = timeSpan.Days - Convert.ToInt32(stepsAdvisor.DeadLineDays);
                     return delay > 0 ? delay : 0;
                 }
             }
             return 0;
         }
    }
}
